//
//  ChoosePickerView.m
//  Init
//
//  Created by zhaoshijie on 2018/5/16.
//  Copyright © 2018 zhaoshijie. All rights reserved.
//

#import "SJChoosePickerView.h"

@implementation SJChoosePickerView

/**
 初始化方法
 
 @param dataArray 数据
 @param completeBlock 选中回调
 @return 对象
 */
- (instancetype)initWithDataArray:(NSArray *)dataArray
                    completeBlock:(CompleteClickBlock)completeBlock
{
    self = [super init];
    if(self)
    {
        
        self.dataArray = dataArray;
        self.completeBlock = completeBlock;
        
        //  初始化视图
        [self initView];
        
    }
    return self;
}

#pragma mark ----------初始化视图----------
//  初始化视图
- (void)initView
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    
    //  创建控件    背景视图
    [self createViewInBgView];
    
    //  创建控件    图标
    [self createViewInHeaderView];
    
    //  创建控件    内容
    [self createViewInPickerView];
    
    //  创建手势    背景的点击手势
    [self createTapInBgView];
    
}




//  创建控件    背景视图
- (void)createViewInBgView
{
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 260+KBottomSurplusHeight)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bgView];
    
    
    
}

//  创建控件    图标
- (void)createViewInHeaderView
{
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 42)];
    self.headerView.backgroundColor = RGBCOLOR16(@"#FEFEFE");
    [self.bgView addSubview:self.headerView];
    
    //  取消
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(0, 0, 75, self.headerView.height);
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelButton setTitleColor:RGBCOLOR16(@"#1B7FCA") forState:UIControlStateNormal];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:cancelButton];
    
    //  完成
    UIButton *completeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    completeButton.frame = CGRectMake(KScreenWidth-80, 0, cancelButton.width, self.headerView.height);
    completeButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [completeButton setTitleColor:RGBCOLOR16(@"1B7FCA") forState:UIControlStateNormal];
    [completeButton setTitle:@"完成" forState:UIControlStateNormal];
    [completeButton addTarget:self action:@selector(completeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:completeButton];
    
    
    //  分割线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.headerView.frame.size.height-0.5, KScreenWidth, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:lineView];
    
}

//  创建控件    内容
- (void)createViewInPickerView
{
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.headerView.frame.size.height, KScreenWidth, self.bgView.frame.size.height-self.headerView.frame.size.height-KBottomSurplusHeight)];
    self.pickerView.delegate = self;
    [self.bgView addSubview:self.pickerView];
    
}


//  创建手势    背景的点击收拾
- (void)createTapInBgView
{
    UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGClick:)];
    tapG.delegate = self;
    [self addGestureRecognizer:tapG];
    
}

#pragma mark --------------方法--------------

/**
 添加
 */
- (void)addToWindow
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.bgView.top = KScreenHeight-260-KBottomSurplusHeight;
        
    }];
}


/**
 移除
 */
- (void)removeToWindow
{
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.bgView.top = KScreenHeight;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
    
}


#pragma mark --------------响应方法--------------

//  取消
- (void)cancelButtonClick:(UIButton *)sender
{
    //  移除
    [self removeToWindow];
}

//  点击确认
- (void)completeButtonClick:(UIButton *)sender
{
    //  移除
    [self removeToWindow];
    
    if(self.completeBlock)
    {
        if(self.index<self.dataArray.count)
        {
            self.completeBlock(self.index);
        }
    }
    
}

//  点击空白区域
- (void)tapGClick:(UITapGestureRecognizer *)tap
{
    //  移除
    [self removeToWindow];
}

#pragma mark ----------UIGestureRecognizerDelegate 代理方法----------

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    UIView *touchView = touch.view;
    
    if([touchView isKindOfClass:[SJChoosePickerView class]])
    {
        return YES;
    }
    
    return NO;
}


#pragma mark ----------UIPickerView 代理方法----------

//返回有几列

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{
    
    return 1;
    
}

//  返回指定列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
    return self.dataArray.count;
}

//  返回指定列，行的高度，就是自定义行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 30;
}


//  设置内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *string = self.dataArray[row];
    
    return string;
    
}


//  字体属性
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *str = self.dataArray[row];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:str];
    
    NSDictionary *attributes_dic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                                     NSForegroundColorAttributeName:[UIColor blackColor]};
    
    [attributedString addAttributes:attributes_dic range:NSMakeRange(0, attributedString.length)];
    
    
    return attributedString;
    
}


//  选中
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    self.index = row;
}



@end
